
<template>
  <div>
    <el-row :gutter="15">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-col :span="22">
          <el-form-item label="章节名称" prop="label">
            <el-input
              v-model="formData.label"
              placeholder="请输入章节名称"
              :maxlength="20"
              show-word-limit
              clearable
              prefix-icon="el-icon-mobile"
              :style="{width: '100%'}"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="课程内容" prop="content">
            <wangEditor v-model="formData.content"></wangEditor>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
import wangEditor from "@/components/wangEditor/Editor";
import { updateChapter,getCatalogue } from "@/api/catalogue";
export default {
  data() {
    return {
      formData: {
        cno: this.$route.query.cno,
        label: this.$route.query.label,
        content: this.$route.query.content,
        id: this.$route.query.chapterId
      },
      rules: {
        label: [
          {
            required: true,
            message: "请输入章节名称",
            trigger: "blur"
          }
        ],
        content: [
          {
            required: true,
            message: "请入输课程内容",
            trigger: "blur"
          }
        ]
      }
    };
  },
  mounted(){
    this.getContent();
  },
  components: { wangEditor },
  methods: {
    submitForm() {
      this.$refs["elForm"].validate(valid => {
        if (!valid) return;
        this.post();
      });
    },
    async getContent(){
      const data = await getCatalogue(this.$route.query.chapterId);
      const res = data.data.data.map.catalogue;
      this.formData.label = res.label;
      this.formData.content = res.content;
    }
    ,
    async post() {
      const { data: res } = await updateChapter(this.formData);
      if (res.code == 20000) {
        this.$notify.success({
          title: "操作成功",
          message: "课程修改成功"
        });
        this.$router.push({ path: "/chapter" });
        // 清空用户信息
        // this.user = {};
      } else {
        return this.$message.error("操作失败:" + this.res.message);
      }
    }
  }
};
</script>
<style scoped>
h1 {
  font-size: 2em;
}
#editor {
  margin-top: 2em;
}
</style>
